home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18324 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  136 lines

  1. Path: telepost.no!usenet
  2. From: "Alf P. Steinbach" <alfps@telepost.no>
  3. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc,comp.os.ms-windows.programmer.tools.misc
  4. Subject: Re: Using CreateProcess
  5. Date: Sat, 20 Apr 1996 01:51:02 +0200
  6. Organization: Telenor Online Public Access
  7. Message-ID: <317826E6.3F1F@telepost.no>
  8. References: <31779265.A80@qc.bell.ca>
  9. NNTP-Posting-Host: monet111.telepost.no
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14.  
  15. Eric Marc Loebenberg wrote:
  16. > Does anyone have an example of using CreateProcess.  I am trying to
  17. > simply use CreateProcess to invoke WORDPAD.EXE but get an Access
  18. > Violation when trying.  Thanks.
  19.  
  20.  
  21. Sorry about tabs in code below, caused by me being lazy (not fixed
  22. setup of DevStudio):
  23.  
  24.  
  25. --------------------------------------------------------------------
  26. #include    <windows.h>
  27.  
  28.  
  29.  
  30. const    char    app_title[]    = "Run A Program";
  31.  
  32.  
  33.  
  34. void ErrorBox( char* s )
  35. {
  36. MessageBox( 0, s, app_title, MB_ICONEXCLAMATION );
  37. }  // ErrorBox
  38.  
  39.  
  40.  
  41. void MyYield()
  42. {
  43. MSG    msg;
  44.  
  45. PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
  46. }  // MyYield
  47.  
  48.  
  49.  
  50. int WINAPI WinMain(
  51.     HINSTANCE    hInstance,        // handle to current 
  52. instance
  53.     HINSTANCE    hPrevInstance,    // handle to previous instance
  54.     LPSTR        lpCmdLine,        // pointer to command 
  55. line
  56.     int            nShowCmd         // show state of 
  57. window
  58.     )    
  59. {
  60. STARTUPINFO            startupinfo    = {sizeof(STARTUPINFO)};
  61. PROCESS_INFORMATION    processinfo    = {0};
  62. int                    result        = 0;
  63.  
  64. if (!CreateProcess(
  65.     NULL,                // pointer to name of executable 
  66. module 
  67.     "C:\\DOC\\MFC-intro\\Test\\mfcrcode\\Release\\mfcrcode.exe",    
  68. // pointer to command line string
  69.     NULL,                // pointer to process security 
  70. attributes 
  71.     NULL,                // pointer to thread security 
  72. attributes 
  73.     FALSE,                // handle inheritance flag 
  74.     0,                    // creation flags 
  75.     NULL,                // pointer to new environment 
  76. block 
  77.     NULL,                // pointer to current directory 
  78. name 
  79.     &startupinfo,        // pointer to STARTUPINFO 
  80.     &processinfo         // pointer to PROCESS_INFORMATION  
  81.     ))
  82. //then
  83.     {
  84.     ErrorBox( "Unable to create process." );
  85.     result = GetLastError();
  86.     }
  87. else
  88.     {  // CreateProcess Ok.
  89.     MyYield();        // Otherwise system is "busy" for a long 
  90. time!
  91.  
  92.     if (WaitForSingleObject( processinfo.hProcess, INFINITE ) != 
  93. WAIT_OBJECT_0)
  94.         {
  95.         ErrorBox( "Wait completed without process termination." 
  96. );
  97.         result = -1;
  98.         }
  99.     else
  100.         {  // WaitForSingleObject Ok.
  101.         unsigned long    exit_code;        // 32-bit.
  102.  
  103.         if (!GetExitCodeProcess( processinfo.hProcess, 
  104. &exit_code))
  105.             {
  106.             ErrorBox( "Unable to obtain process exit code." 
  107. );
  108.             result = -1;
  109.             }
  110.         else
  111.             {  // GetExitCodeProcess Ok.
  112.             char    s[256];
  113.             wsprintf( s, "Process exited with code %lu", 
  114. exit_code );
  115.             MessageBox( 0, s, app_title, MB_ICONINFORMATION 
  116. );
  117.  
  118.             if (!CloseHandle( processinfo.hProcess ) )
  119.                 {  // CloseHandle Ok.
  120.                 ErrorBox( "Unable to close process 
  121. handle." );
  122.                 }  // CloseHandle Ok.
  123.             }  // GetExitCodeProcess Ok.
  124.         }  // WaitForSingleObject Ok.
  125.     }  // CreateProcess Ok.
  126. return result;
  127. }  // WinMain
  128. ----------------------------------------------------------------------
  129.  
  130.  
  131. Hope this helps,
  132.  
  133.  
  134. - Alf
  135.